package com.bilicraft.plugin.kexue.commands;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import com.bilicraft.plugin.kexue.ShellUser;

public class BiliCmdCat extends BiliCmd{

	public BiliCmdCat(){
		nofArgs = 1;
	}
	
	public boolean doExec(CommandSender sender, Command cmd, String label, String[] args, ShellUser shellUser){
		try{
			File fdir = new File(shellUser.curdir);
			shellUser.curdir = fdir.getCanonicalPath();
			File cfile = new File(fdir, args[0]);
			if(!cfile.exists()){
				sender.sendMessage("File not found!");
				return true;
			}
			if(!cfile.canRead()){
				sender.sendMessage("Not allowed!");
				return true;
			}
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(cfile)));
			String s = br.readLine();
			while(s != null){
				sender.sendMessage("[C]" + s);
				s = br.readLine();
			}
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
}
